<?php
/*
 * 主要功能:
 *
 * dreamxyp(QQ:31996798) - PHP - class.img.php
 * coding:夏一平
 * 創建時間:2006-11-2
 */
 class img
 {
 	static protected $data = array(
			1 => 'gif',2 => 'jpg',3 => 'png',4 =>'swf',5 =>'psd',6 => 'bmp',7 =>'tiff',8 =>'tiff',
			9 => 'jpc',10=> 'jp2',11 =>'jpx',12 =>'jb2',13 =>'swc',14 =>'iff',15 =>'wbmp',16 =>'xbm'
		);
 	/*
 	 * 功能:圖片縮放
 	 */
 	static public function zoom($file,$sw=160,$sh=90,$outType=0,$quality=95,$outFile='')
 	{
 		$info 		= self::readImage($file);
 		$outFile	= $outFile?$outFile:$file;
 		if($info[0])
 		{
 			$info		= $info[1];
 			$newInfo	= self::c_zoom($info['w'],$info['h'],$sw,$sh);
 			if($newInfo[0])
 			{
 				$im	=	self::disposalImage($info['im'],$newInfo[1]);
 				return self::outImage($outFile,$im,$outType,$quality);
 			}else if($outType)
 			{
 				return self::outImage($outFile,$info['im'],$outType,$quality);
 			}else
 			{
 				if($outFile != $file) copy($file,$outFile);
 				return array(1,$file);
 			} 			
 		}else
 		{//失敗
 			return $info;
 		} 		
 	}
	/*
 	 * 功能:圖片剪切
 	 */
 	static public function cut($file,$sw=160,$sh=120,$outType=0,$quality=95,$outFile='')
 	{
 		$info 		= self::readImage($file);
 		$outFile	= $outFile?$outFile:$file;
 		if($info[0])
 		{
 			$info		= $info[1];
 			$newInfo	= self::c_cut($info['w'],$info['h'],$sw,$sh);
 			$im			= self::disposalImage($info['im'],$newInfo);
 			return self::outImage($outFile,$im,$outType,$quality);
 		}else
 		{//失敗
 			return $info;
 		} 
 	}
 	/*
 	 * 功能:圖片疊加$splice加在$file上 輸出$file
 	 */
 	static public function splice($file,$splice='',$sw=0,$sh=0,$outType=0,$quality=95,$outFile='')
 	{
 		$info 				= self::readImage($file);
 		$outFile			= $outFile?$outFile:$file;
 		if($info[0])
 		{
 			$info		= $info[1];
 			$sw			= $sw?$sw:$info['w'];
 			$sh			= $sh?$sh:$info['h'];
 			$newInfo	= self::c_cut($info['w'],$info['h'],$sw,$sh);
 			$im			= self::disposalImage($info['im'],$newInfo);
 			if($splice)
 			{
	 			if(!is_array($splice))
	 			{
	 				$splice = array($splice);
	 			}
	 			foreach($splice as $v)
	 			{
	 				$infoT = self::readImage($v);
	 				if($infoT[0])
	 				{
	 					$infoT 		= $infoT[1];
	 					$newInfoT	= self::c_cut($infoT['w'],$infoT['h'],$sw,$sh);
	 					$imT		= self::disposalImage($infoT['im'],$newInfoT,false);
	 					imagecopymerge ($im,$imT,0,0, 0,0,$sw,$sh,100);
	 				}
	 			}
 			}
 			return self::outImage($outFile,$im,$outType,$quality);
 		}else
 		{//失敗
 			return $info;
 		}
 	}
 	/*
 	 * 功能:檢測支持文件類弄
 	 */
	static public function checkType()
	{
		$data = array(
			"gif"	=>	"GIF Read Support",
 			"gif_c"	=>	"GIF Create Support",
 			"jpg"	=>	"JPG Support",
 			"png"	=>	"PNG Support",
 			"wbmp"	=>	"WBMP Support",
 			"xbm"	=>	"XBM Support"
		);
		$rs		=array();
		$info 	= gd_info();
		foreach($data as $k => &$v)
		{
			if($info[$v])$rs[] = $k;	
		}
		return $rs;				
	}
	/*
 	 * 功能:讀出圖片 
 	 */
 	protected function readImage($file)
 	{
 		$Support = self::checkType();
		list($width, $height, $type, $attr) = @getimagesize($file);
		$rs		 = array(
			'w' => $width,
			'h' => $height
		);
 		if(in_array(self::$data[$type],$Support))
 		{
 			if($type == 2)
 			{
 				$rs['im'] = imagecreatefromjpeg($file);
 			}else if($type == 1)
 			{
 				$rs['im'] = imagecreatefromgif($file);
 			}else if($type == 3)
 			{
 				$rs['im'] = imagecreatefrompng($file);
 			}else if($type == 15)
 			{
 				$rs['im'] = imagecreatefromwbmp($file);
 			}else if($type == 16)
 			{
 				$rs['im'] = imagecreatefromxbm($file);
 			}
 			if($rs['im'])
 			{
 				return array(1,$rs);
 			}
 		}
		return array(0,'失敗:服務器對'.self::$data[$type].'格式文件不支持!');		
 	}
 	protected function c_zoom($w,$h,$sw,$sh)
 	{
 		if($sw && $w > $sw)
	    { 
		   $wr 	= $sw/$w; 
		   $rw	= true; 
	    } 
	    if($sh && $h > $sh)
	    { 
		   $hr = $sh/$h; 
		   $rh = true; 
	    }
	    #
	    if($rw && $rh)
	    { 
		   $ratio =($wr < $hr)?$wr:$hr;
		}elseif($rw)
	    { 
		   $ratio = $wr; 
	    }elseif($rh)
	    { 
		   $ratio = $hr; 
	    }else
	    {
	    	return array(0,'提示:本圖片不用縮放!');
	    }
	    return array(1,array(
	    	'ow'=>$w,
	    	'oh'=>$h,
			'w'	=> $w * $ratio,
	    	'h'	=> $h * $ratio,
	    	'x'	=> 0,
	    	'y'	=> 0
	    ));
 	}
 	protected function c_cut($w,$h,$sw,$sh)
 	{
 		if(($sw/$w)<=($sh/$h))
		{
			$x	=($w-$h*$sw/$sh)/2;
			$y	=0;
			$w	=$h*$sw/$sh;
		}else
		{
			$x	=0;
			$y	=($h-$w*$sh/$sw)/2;
			$h		=$w*$sh/$sw;
		}
		return array(
	    	'ow'=>$w,
	    	'oh'=>$h,
			'w'	=>$sw,
	    	'h'	=>$sh,
	    	'x'	=>$x,
	    	'y'	=>$y
	    );
 	}
 	protected function disposalImage($im,$Info,$bgColor=true)
 	{
 		if(function_exists("imagecopyresampled"))
		{ 
			$newim = imagecreatetruecolor($Info['w'], $Info['h']);
			if($bgColor)
			{
				//imagecolortransparent($newim,imagecolorallocate($newim, 0, 0, 0));
				imagefill($newim, 0, 0, imagecolorallocate($newim, 255, 255, 255));  
			}else
			{
				imagecolortransparent($newim,imagecolorallocate($newim, 0, 0, 0));
			}
			imagecopyresampled($newim, $im, 0, 0, $Info['x'], $Info['y'], $Info['w'], $Info['h'], $Info['ow'], $Info['oh']); 
		}else
		{ 
			$newim = imagecreate($Info['w'], $Info['h']); 
			if($bgColor)
			{
				imagefill($newim, 0, 0, imagecolorallocate($newim, 255, 255, 255)); 
			}else
			{
				imagecolortransparent($newim,imagecolorallocate($newim, 0, 0, 0));
			}
			imagecopyresized($newim, $im, 0, 0, $Info['x'], $Info['y'], $Info['w'], $Info['h'], $Info['ow'], $Info['oh']); 
		}
		ImageDestroy ($im);
		return $newim;
 	}
 	protected function outImage($file,$im,$outType,$quality)
 	{
 		/*$fileArray	 = g::basename($file,2);
 		if($outType)
 		{
 			$outType = self::$data[$outType]?self::$data[$outType]:$outType;
 			$file	 = $fileArray['dir'].$fileArray['name'].'.'.$outType; 			
 		}else
 		{
 			if(in_array($fileArray['ex'],array('jpg','jpeg','gif', 'png','wbmp')))
 			{
 				$outType = $fileArray['ex'];
 			}else
 			{
 				$outType = 'jpg';
 			}
 		}*/
		
 		if($outType == 'jpg' || $outType == 'jpeg' ||  $outType == 2)
		{
			imagejpeg($im,$file,$quality);
		}else if($outType == 'gif' ||  $outType == 1)
		{
			imagegif($im,$file,$quality);
		}else if($outType == 'png' ||  $outType ==3)
		{
			imagepng($im,$file,$quality);
		}else if($outType == 'wbmp' ||  $outType == 15)
		{
			imagewbmp($im,$file,$quality);
		}else
		{
			return array(0,'失敗:系統不支持能輸出"'.$outType.'"圖片格式!');
		}
 		return array(1,$file);
  	}	
}
?>
